﻿using System;
using System.Collections.Generic;
using System.Text;
using ERP.BusinessEntities.Base;
using System.IO;
using ERP.DAL.Base;
using ERP.DAL.Util;
using System.Windows.Forms;
using ERP.BusinessEntities.Util;

namespace ERP.BusinessEntities.Factory
{
    public class BusinessFactory
    {
        public static Dictionary<String, InstantiateObjectHandler> lstInitInfo = new Dictionary<string, InstantiateObjectHandler>();
        public static Dictionary<String, InstantiateObjectHandler> lstInitCtrls = new Dictionary<string, InstantiateObjectHandler>();

        public static String AssemplyName = "ERP.BusinessEntities";
        public static Object CreateBusinessObject(String strBusinessName)
        {
            InstantiateObjectHandler InitObj = null;
            if (lstInitInfo.TryGetValue(strBusinessName, out InitObj) == false)
            {
                String strFullName = AssemplyName + "." + strBusinessName + ", ERP.BusinessEntities";
                Type ObjectType = Type.GetType(strFullName);
                if (ObjectType == null)
                    return null;
                InitObj = DynamicMethodCompiler.CreateInstantiateObjectHandler(ObjectType);
                lstInitInfo.Add(strBusinessName, InitObj);
            }

            return InitObj();
        }
        public static Object CreateBusinessObject(Type ObjectType)
        {
            InstantiateObjectHandler InitObj = null;
            if (lstInitInfo.TryGetValue(ObjectType.Name, out InitObj) == false)
            {
                InitObj = DynamicMethodCompiler.CreateInstantiateObjectHandler(ObjectType);
                lstInitInfo.Add(ObjectType.Name, InitObj);
            }

            return InitObj();
        }

        public static Object CreateBusinessCtrl(String strBusinessName)
        {
            InstantiateObjectHandler InitObj = null;
            if (lstInitCtrls.TryGetValue(strBusinessName, out InitObj) == false)
            {
                String strFullName = AssemplyName + "." + strBusinessName + ", ERP.BusinessEntities"; ;
                Type ObjectType = Type.GetType(strFullName);
                if (ObjectType == null)
                    return null;
                InitObj = DynamicMethodCompiler.CreateInstantiateObjectHandler(ObjectType);
                lstInitCtrls.Add(strBusinessName, InitObj);
            }
            
            return InitObj();
        }
        public static Object CreateBusinessCtrl(Type ObjectType)
        {
            InstantiateObjectHandler InitObj = null;
            if (lstInitCtrls.TryGetValue(ObjectType.Name, out InitObj) == false)
            {
                InitObj = DynamicMethodCompiler.CreateInstantiateObjectHandler(ObjectType);
                lstInitCtrls.Add(ObjectType.Name, InitObj);
            }

            return InitObj();
        }


        public static String GenerateBusinessInfo(String strTable)
        {
            IDBUtil DBUtil = new SQLDBUtil();
            StringBuilder strBuilderInfo = new StringBuilder();

            #region Using
            strBuilderInfo.Append("using System;\nusing System.Collections.Generic;\nusing System.Text;\nusing ERP.BusinessEntities.Base;\n");
            #endregion

            #region NameSpace
            strBuilderInfo.Append("namespace ERP.BusinessEntities\n{\n");

            #region ClassName
            String strInfoName = strTable + "Info";
            String strClassName = String.Format("\tpublic class {0}:BaseBusinessInfo", strInfoName);
            strBuilderInfo.Append(strClassName + "\n\t{\n");

            List<String> lstColumnName = DBUtil.GetListColumnNameOfTable(strTable);
            #region Variable
            strBuilderInfo.Append("\t\t#region Variable\n");
            foreach (String strColumnName in lstColumnName)
            {
                String privateName = strColumnName.Substring(0, 1).ToLower() + strColumnName.Substring(1, strColumnName.Length - 1);
                String strColumnType = DBUtil.GetTypeOfColumnOnTable(strTable, strColumnName).Key;
                int strColumnLenght = DBUtil.GetTypeOfColumnOnTable(strTable, strColumnName).Value;
                String strVar = String.Empty;

                if (strColumnType.Contains("int"))
                {
                    strVar = String.Format("\t\tprivate int _{0};", privateName);
                    strBuilderInfo.Append(strVar + "\n");
                    continue;
                }
                if (strColumnType.Contains("float"))
                {
                    strVar = String.Format("\t\tprivate double _{0};", privateName);
                    strBuilderInfo.Append(strVar + "\n");
                    continue;
                }
                if (strColumnType.Contains("real"))
                {
                    strVar = String.Format("\t\tprivate double _{0};", privateName);
                    strBuilderInfo.Append(strVar + "\n");
                    continue;
                }
                if (strColumnType.Equals("money"))
                {
                    strVar = String.Format("\t\tprivate double _{0};", privateName);
                    strBuilderInfo.Append(strVar + "\n");
                    continue;
                }
                if (strColumnType.Equals("bit"))
                {
                    strVar = String.Format("\t\tprivate bool _{0};", privateName);
                    strBuilderInfo.Append(strVar + "\n");
                    continue;
                }
                if (privateName.Contains("AStatus"))
                {
                    strVar = String.Format("\t\tprivate String _{0} = \"Alive\";", privateName);
                    strBuilderInfo.Append(strVar + "\n");
                    continue;
                }
                if (strColumnType.Contains("char") || strColumnType.Contains("ntext"))
                {
                    strVar = String.Format("\t\tprivate String _{0} = String.Empty;", privateName);
                    strBuilderInfo.Append(strVar + "\n");
                    continue;
                }
                
                if (strColumnType.Contains("datetime"))
                {
                    if (strColumnLenght < 0)
                        strVar = String.Format("\t\tprivate DateTime _{0} = DateTime.Now;", privateName);
                    else
                        strVar = String.Format("\t\tprivate DateTime? _{0} = null;", privateName);

                    strBuilderInfo.Append(strVar + "\n");
                    continue;
                }
                if (strColumnType.Contains("image"))
                {
                    strVar = String.Format("\t\tprivate byte[] _{0};", privateName);
                    strBuilderInfo.Append(strVar + "\n");
                    continue;
                }
            }
            strBuilderInfo.Append("\t\t#endregion\n\n\n");
            #endregion

            #region Properties
            strBuilderInfo.Append("\t\t#region Properties\n");
            foreach (String strColumnName1 in lstColumnName)
            {
                String privateName1 = strColumnName1.Substring(0, 1).ToLower() + strColumnName1.Substring(1, strColumnName1.Length - 1);
                String strColumnType = DBUtil.GetTypeOfColumnOnTable(strTable, strColumnName1).Key;
                int strColumnLenght = DBUtil.GetTypeOfColumnOnTable(strTable, strColumnName1).Value;
                String strVar = String.Empty;

                if (strColumnType.Contains("int"))
                {
                    strVar = String.Format("\t\tpublic int {0}", strColumnName1);
                    strBuilderInfo.Append(strVar + "\n\t\t{\n");
                }

                if (strColumnType.Contains("float"))
                {
                    strVar = String.Format("\t\tpublic double {0}", strColumnName1);
                    strBuilderInfo.Append(strVar + "\n\t\t{\n");
                }
                if (strColumnType.Contains("real"))
                {
                    strVar = String.Format("\t\tpublic double {0}", strColumnName1);
                    strBuilderInfo.Append(strVar + "\n\t\t{\n");
                }
                if (strColumnType.Contains("money"))
                {
                    strVar = String.Format("\t\tpublic double {0}", strColumnName1);
                    strBuilderInfo.Append(strVar + "\n\t\t{\n");
                }
                if (strColumnType.Equals("bit"))
                {
                    strVar = String.Format("\t\tpublic bool {0}", strColumnName1);
                    strBuilderInfo.Append(strVar + "\n\t\t{\n");
                }
                if (strColumnType.Equals("datetime"))
                {
                    if (strColumnLenght < 0)
                        strVar = String.Format("\t\tpublic DateTime {0}", strColumnName1);
                    else
                        strVar = String.Format("\t\tpublic DateTime? {0}", strColumnName1);
                    strBuilderInfo.Append(strVar + "\n\t\t{\n");
                }
                if (strColumnType.Contains("char"))
                {
                    strVar = String.Format("\t\tpublic String {0}", strColumnName1);
                    strBuilderInfo.Append(strVar + "\n\t\t{\n");
                }
                if (strColumnType.Contains("ntext"))
                {
                    strVar = String.Format("\t\tpublic String {0}", strColumnName1);
                    strBuilderInfo.Append(strVar + "\n\t\t{\n");
                }
                if (strColumnType.Contains("image"))
                {
                    strVar = String.Format("\t\tpublic byte[] {0}", strColumnName1);
                    strBuilderInfo.Append(strVar + "\n\t\t{\n");
                }
                strBuilderInfo.Append("\t\t\tget { return _" + privateName1 + ";}\n");
                String strSet = "\t\t\tset\n\t\t\t{\n\t\t\t\t_" + privateName1 + " = value;\n\t\t\t\tthis.NotifyPropertyChanged(\"" + strColumnName1 + "\");\n\t\t\t}\n";
                strBuilderInfo.Append(strSet);
                strBuilderInfo.Append("\t\t}\n");
            }
            strBuilderInfo.Append("\t\t#endregion\n");
            #endregion

            strBuilderInfo.Append("\t}\n");
            #endregion

            strBuilderInfo.Append("}");
            #endregion

            return strBuilderInfo.ToString();
        }
        public static String GenerateBusinessInfo(String strTable, String strFolderPath)
        {
            String strInfo = GenerateBusinessInfo(strTable);
            if (!strFolderPath.EndsWith("\\"))
                strFolderPath += "\\";
            String strFilePath = strFolderPath + strTable + "Info.cs";
            FileInfo fInfo = new FileInfo(strFilePath);
            if (fInfo.Exists == true)
            {
                DialogResult dlg = MessageBox.Show("Replace Existed file ?", "Exited File", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dlg.Equals(DialogResult.Yes))
                {
                    StreamWriter strWriter = new StreamWriter(strFilePath);
                    
                    strWriter.Write(strInfo);
                    strWriter.Flush();
                    strWriter.Close();
                }
            }else
            {
                StreamWriter strWriter = new StreamWriter(strFilePath);

                strWriter.Write(strInfo);
                strWriter.Flush();
                strWriter.Close();
            }
            return strInfo;
        }

        public static String GenerateBusinessCtrl(String strTable)
        {
            StringBuilder strBuilderCtrl = new StringBuilder();
            String strClassName = strTable + "Controller";
            strBuilderCtrl.Append("using System;\nusing System.Collections.Generic;\nusing System.Text;\nusing ERP.BusinessEntities.Base;\nusing ERP.DAL.Factory;\n");

            strBuilderCtrl.Append("namespace ERP.BusinessEntities\n{" + "\n\n");

            strBuilderCtrl.Append("\tpublic class " + strClassName + ":BaseBusinessController" + "\n\t{\n");

            strBuilderCtrl.Append("\t\tpublic " + strClassName + "()\n\t\t{\n");

            strBuilderCtrl.Append("\t\t\tdal = FactoryDal.CreateEndDal(\"" + strTable + "\");\n");
            strBuilderCtrl.Append("\t\t}\n");

            strBuilderCtrl.Append("\t}\n");

            strBuilderCtrl.Append("}\n");

            return strBuilderCtrl.ToString();
        }

        public static String GenerateBusinessCtrl(String strTable, String strFolderPath)
        {
            String strCtrl = GenerateBusinessCtrl(strTable);
            if (!strFolderPath.EndsWith("\\"))
                strFolderPath += "\\";
            String strFilePath = strFolderPath + strTable + "Controller.cs";
            FileInfo fInfo = new FileInfo(strFilePath);
            if (fInfo.Exists == true)
            {
                DialogResult dlg = MessageBox.Show("Replace Existed file ?", "Exited File", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dlg.Equals(DialogResult.Yes))
                {
                    StreamWriter strWriter = new StreamWriter(strFilePath);

                    strWriter.Write(strCtrl);
                    strWriter.Flush();
                    strWriter.Close();
                }
            }
            else
            {
                StreamWriter strWriter = new StreamWriter(strFilePath);

                strWriter.Write(strCtrl);
                strWriter.Flush();
                strWriter.Close();
            }
            return strCtrl;
        }


    }
}
